package cn.mymall.web.controller.houtai.mall;

import cn.mymall.entity.mall.Category;
import cn.mymall.entity.mall.SubCategory;
import cn.mymall.service.mall.ICategoryService;
import cn.mymall.service.mall.ISubCategoryService;
import cn.mymall.support.ResultCode;
import cn.mymall.util.BaseWebUtil;
import cn.mymall.util.BindUtils;
import cn.mymall.util.DateUtil;
import cn.mymall.web.controller.houtai.BaseHoutaiController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SubCategoryApiController extends BaseHoutaiController {
    @Autowired
    private ISubCategoryService subCategoryService;
    @Autowired
    private ICategoryService categoryService;


    @RequestMapping("/api/subcategory/list")
    public ResultCode subCategoryList(Integer categoryId) {
        Category category = categoryService.getById(categoryId);
        List<SubCategory> list = Lists.newArrayList(subCategoryService.list(new QueryWrapper<SubCategory>().eq("category_id", categoryId)));
        Collections.sort(list, new PropertyComparator<SubCategory>("sortNum", false, true));
        Map map = new HashMap();
        map.put("subCategoryList", list);
        map.put("category", category);
        return ResultCode.getSuccessReturn(map);
    }

    @RequestMapping("/api/subcategory/setSortNum")
    @ResponseBody
    public ResultCode setSubcategory(Integer id, Integer sortNum) {
        SubCategory obj = subCategoryService.getById(id);
        if (sortNum == null) {
            return ResultCode.getFailure("请输入排序");
        }
        obj.setSortNum(sortNum);
        subCategoryService.updateById(obj);
        return ResultCode.SUCCESS;
    }

    @RequestMapping("/api/subcategory/get")
    public ResultCode modifySubcategory(Integer id, Integer categoryId) {
        SubCategory obj = new SubCategory();
        if (id != null) {
            obj = subCategoryService.getById(id);
        } else {
            obj.setCategoryId(categoryId);
        }
        return ResultCode.getSuccessReturn(obj);
    }

    @RequestMapping("/api/subcategory/save")
    public ResultCode saveSubcategory(Integer id, Integer categoryId) {
        SubCategory obj = null;
        if (id != null) {
            obj = subCategoryService.getById(id);
        } else {
            obj = new SubCategory();
            obj.setCategoryId(categoryId);
            obj.setSortNum(1);
            obj.setAddTime(DateUtil.getCurFullTimestamp());
        }

        BindUtils.bindData(obj, BaseWebUtil.getRequestParams());
        subCategoryService.saveOrUpdate(obj);
        return ResultCode.SUCCESS;
    }

    //分类联动
    @RequestMapping("/api/subcategory/listById")
    public ResultCode<List<SubCategory>> listBySubCategoryId(Integer categoryId) {
        QueryWrapper<SubCategory> ew = new QueryWrapper<SubCategory>();
        if (categoryId != null) {
            ew.eq("category_id", categoryId);
        }
        ew.orderByAsc("id");
        List<SubCategory> list = subCategoryService.list(ew);
        return ResultCode.getSuccessReturn(list);
    }

}
